﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Deanery.Controller.Directories;

namespace Deanery.Controller.PersonData
{
    class CityController
    {
        private readonly deaneryEntities _db = new deaneryEntities();

        private string[] Divider(string str)
        {
            var split = str.Split('.');
            return split;
        }

        /// <summary>
        /// This method find reccord guid by name and guid of city_type in table City
        /// </summary>
        /// <param name="name">string value to search reccord in table</param>
        /// <param name="region">string value to search reccord in table</param>
        ///  <returns>guid value of finded reccord</returns>
        
        public Guid FindGuidByCityName(string region, string name)
        {
            var spliter = Divider(name.Trim());
            var typeCity = spliter[0];
            var nameCity = spliter[1];
            var typeCityObj = new TypeCityController();
            var typeRegionObj = new RegionController();
            var guidTypeCity = typeCityObj.FindGuidByName(typeCity);
            var guidRegion = typeRegionObj.FindGuidByName(region);

            var obj = (_db.Cities.Where(t => t.nv_name == nameCity && t.id_type_city == guidTypeCity && 
                t.id_region == guidRegion).Select(t => t.id_city)).SingleOrDefault();

            return obj;
        }

        /// <summary>
        /// This method select all cityes name from table City
        /// idea of this method: to return data from all conected tables with this one
        /// </summary>
        /// <returns>Array(every item is city name)</returns>
        public Array GetConectedData()
        {
            var list = from t in _db.Cities select t.nv_name;
            return list.ToArray();
        }

        /// <summary>
        /// This method performs INSERT one record to DB in table City
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param> 
        public void Insert(ArrayList reccord)
        {
            var typeCityObj = new TypeCityController();
            var typeRegionObj = new RegionController();

            var obj = new City
                {
                    id_city = new Guid(),
                    id_region = typeRegionObj.FindGuidByName(reccord[0].ToString()),
                    id_type_city = typeCityObj.FindGuidByName(reccord[1].ToString()),
                    nv_name = reccord[2].ToString()
                };
            if (3 < reccord.Count) obj.int_code_KOATYY = (int) reccord[3];
            if (4 < reccord.Count) obj.nv_full_name = reccord[4].ToString();
            
            _db.Cities.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method for import
        /// performs INSERT one record to DB in table City
        /// </summary>
        /// <param name="region">string value of region name</param>
        /// <param name="name">string  value of city type and city (c.Rivne)</param>
        public void Insert(string region, string name)
        {
            var spliter = Divider(name.Trim());

            var typeCityObj = new TypeCityController();
            var typeRegionObj = new RegionController();

            var obj = new City
            {
                id_city = Guid.NewGuid(),
                id_region = typeRegionObj.FindGuidByName(region),
                id_type_city = typeCityObj.FindGuidByName(spliter[0]),
                nv_name = spliter[1]
            };
           
            _db.Cities.AddObject(obj);
            _db.SaveChanges();
        }
    }
}
